package com.zeta.eduservice.utils.pdf;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.question.EduQuestionTypeSys;
import com.zeta.eduservice.entity.vo.front.PdfQuestionVo;
import com.zeta.eduservice.entity.vo.front.QuestionCreateInfo;
import com.zeta.eduservice.entity.vo.front.front.PaperInfoVo;
import com.zeta.eduservice.mapper.EduQuestionChoiceMapper;
import com.zeta.eduservice.mapper.EduQuestionMapper;
import com.zeta.eduservice.service.EduQuestionChoiceService;
import com.zeta.eduservice.service.EduQuestionService;
import com.zeta.eduservice.service.EduQuestionTypeSysService;

@Component
public class PdfUtils {
	
	@Autowired
	EduQuestionTypeSysService eduQuestiontypeService;
	
	@Autowired
	EduQuestionChoiceMapper questionChoiceMapper;
	@Autowired
	EduQuestionMapper questionMapper;
	BaseFont baseFont;

	
	public PdfUtils() throws DocumentException, IOException {
		baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	}

	public void creatPdf(String path, List<PdfQuestionVo> list) throws DocumentException, IOException {
		Document document = new Document(PageSize.A4);
		// 第二步 获取PdfWriter实例
		PdfWriter.getInstance(document, new FileOutputStream(path));
		// 第三步 打开文档
		document.open();
		BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font font = new Font(baseFont, 10, Font.BOLD);

		Paragraph paragraph = new Paragraph();

		for (PdfQuestionVo i : list) {
			Chunk chunk = new Chunk(i.getContent() + "\n", font);

			paragraph.add(chunk);
		}

		document.add(paragraph);
		document.close();

	}

	public void creatPdfPaper(String path, List<QuestionCreateInfo> list, PaperInfoVo paperInfo)
			throws DocumentException, IOException {

		Font titleF = new Font(baseFont, 18, Font.BOLD);
		Font coursetitleF = new Font(baseFont, 16, Font.BOLD);
		Font classtitleF = new Font(baseFont, 12, Font.BOLD);
		Document document = new Document(PageSize.A4);
		document.setMargins(22, 56, 56, 56);

		PdfWriter.getInstance(document, new FileOutputStream(path));
		document.open();

		Paragraph p1 = new Paragraph(20);
		Phrase phrase = new Phrase();

		phrase.add(new Chunk(paperInfo.getPapertitle(), titleF));
		phrase.add(Chunk.NEWLINE);
		phrase.add(new Chunk(paperInfo.getTesttitle(), coursetitleF));
		phrase.add(Chunk.NEWLINE);
		phrase.add(new Chunk("(" + paperInfo.getClasstitle() + ")", classtitleF));
		phrase.add(Chunk.NEWLINE);
		p1.setAlignment(Element.ALIGN_CENTER);
		p1.add(phrase);

		document.add(p1);
		Paragraph p2 = new Paragraph();
		p2.setSpacingBefore(5f);
		p2.setSpacingAfter(5f);
		PdfPTable table = new PdfPTable(list.size() + 3);
		p2.setSpacingBefore(3);
		p2.setSpacingAfter(3);
		table.addCell(new Phrase("题号", classtitleF));
		String[] s = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四" };
		for (int i = 0; i < list.size(); i++) {
			table.addCell(new Phrase(s[i], classtitleF));
		}
		table.addCell(new Phrase("总分", classtitleF));
		table.addCell(new Phrase("复核人", classtitleF));

		table.addCell(new Phrase("得分", classtitleF));
		for (int i = 0; i <= list.size() + 1; i++) {
			table.addCell(" ");
		}
		table.addCell(new Phrase("阅卷人", classtitleF));
		for (int i = 0; i <= list.size() + 1; i++) {
			table.addCell(" ");
		}
		p2.add(table);
		document.add(p2);
		int n=0;
		for (QuestionCreateInfo i : list) {
			Paragraph paragraph = new Paragraph(22);
			if (i.getType().equals("选择")) {
				QueryWrapper<EduQuestionTypeSys> queryWrapper=new QueryWrapper<EduQuestionTypeSys>();
				queryWrapper.eq("type", "选择");
				EduQuestionTypeSys obj =  eduQuestiontypeService.getOne(queryWrapper);
				Phrase phrase1 = new Phrase();
					phrase1.add(new Chunk(s[n++]+"、"+ obj.getName()+"（每小题"+i.getScore()+"分，共"+i.getNumber()*i.getScore()+"分）",titleF));
					phrase1.add(Chunk.NEWLINE);
				Phrase phrase2 = new Phrase();
				List<EduQuestionChoice> rand = questionChoiceMapper.getByRandom(i.getDifficulty(),i.getNumber());
				int num=1;
				for(EduQuestionChoice j:rand) {
					phrase2.add(new Chunk(num++ +"."+j.getContent(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
					phrase2.add(new Chunk("A."+j.getChoiceA(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
					phrase2.add(new Chunk("B."+j.getChoiceB(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
					phrase2.add(new Chunk("C."+j.getChoiceC(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
					phrase2.add(new Chunk("D."+j.getChoiceD(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
				}
			paragraph.add(phrase1);
			paragraph.add(phrase2);
			
			}else if (i.getType().equals("填空")) {
				QueryWrapper<EduQuestionTypeSys> queryWrapper=new QueryWrapper<EduQuestionTypeSys>();
				queryWrapper.eq("type", "填空");
				EduQuestionTypeSys obj =  eduQuestiontypeService.getOne(queryWrapper);
				Phrase phrase1 = new Phrase();
				phrase1.add(new Chunk(s[n++]+"、"+ obj.getName()+"（每小题"+i.getScore()+"分，共"+i.getNumber()*i.getScore()+"分）",titleF));
				phrase1.add(Chunk.NEWLINE);
				Phrase phrase2 = new Phrase();
				List<PdfQuestionVo> byRand = questionMapper.getByRand(i.getType(), i.getDifficulty(), i.getNumber());
				int num=1;
				for(PdfQuestionVo j:byRand) {
					phrase2.add(new Chunk(num++ +"。"+j.getContent(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
				}
			paragraph.add(phrase1);
			paragraph.add(phrase2);
			}else if (i.getType().equals("判断")) {
				QueryWrapper<EduQuestionTypeSys> queryWrapper=new QueryWrapper<EduQuestionTypeSys>();
				queryWrapper.eq("type", "判断");
				EduQuestionTypeSys obj =  eduQuestiontypeService.getOne(queryWrapper);
				Phrase phrase1 = new Phrase();
				phrase1.add(new Chunk(s[n++]+"、"+ obj.getName()+"（每小题"+i.getScore()+"分，共"+i.getNumber()*i.getScore()+"分）",titleF));
				phrase1.add(Chunk.NEWLINE);
				Phrase phrase2 = new Phrase();
				List<PdfQuestionVo> byRand = questionMapper.getByRand(i.getType(), i.getDifficulty(), i.getNumber());
				int num=1;
				for(PdfQuestionVo j:byRand) {
					phrase2.add(new Chunk(num++ +"。"+j.getContent(), classtitleF));
					phrase2.add(Chunk.NEWLINE);
				}
				
			}
		
			
		document.add(paragraph);
		}

		document.close();

	}

}
